<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%String path = request.getContextPath() ;%>
<%@ taglib uri="/WEB-INF/tld/c.tld" prefix="c" %> 
<!DOCTYPE html>
<html lang="zh-CN" >
<head>
<c:set var="ctx" value="${pageContext.request.contextPath}" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>保证金退回</title>
<jsp:include page="../../import.jsp" flush="true" />
<style type="text/css">
</style>
</head>
<body class="iframeBody">
<div class="Wrapper">
     <div class="fieldset1 tabel_col tabel_col_s clearFix">
        <div class="caption">
			<span class="title">保证金基础信息 </span>
		</div>
		<div class="fieldBox col_33">
		   <div class="label">项目名称</div>
		   <div class="field">
              <span id="fncPrjNm" ></span>
           </div>
		</div>
		<div class="fieldBox col_33">
		   <div class="label">项目编号</div>
		   <div class="field">
              <span id="fncPrjIdShow" ></span>
           </div>
		</div>
		<div class="fieldBox col_33">
		   <div class="label">融资机构</div>
		   <div class="field">
              <span id="fncInsNm" ></span>
           </div>
		</div>
		<div class="fieldBox col_33">
		   <div class="label">已付保证金</div>
		   <div class="field">
              <span id="mrgnAtm" ></span> 
           </div>
		</div>
		<div class="fieldBox col_33">
		   <div class="label">付款时间</div>
		   <div class="field">
             <span id="payTm"></span> 
           </div>
		</div>
		<div class="fieldBox col_33">
		   <div class="label">剩余保证金</div>
		   <div class="field">
		     <span id="srplsMrgnShow"></span>
           </div>
		</div>
	</div>
	 <div class="fieldset1 tabel_col_s clearFix" >
	   <div class="caption">
		   <span class="title" >保证金使用记录</span>
	   </div>
	  <div id="mrgnReturnligergrid" class="sq-grid"></div>
	</div><br/> 
	<form id="formMrgnReturnInfo">
	<div class="fieldset1 tabel_col tabel_col_s clearFix" >
	    <div class="caption">
			<span class="title">保证金退回信息 </span>
		</div>
	   <div class="fieldBox col_50">
		   <div class="label">申请退回保证金</div>
		   <div class="field">
		     <input name="txnAmt" id="txnAmt" type="text" class="ui-textbox" value="${txnAmt}" validate="{required:true, maxlength:20, number:true}"> 
           </div>
		</div>
		<div class="fieldBox col_50">
		   <div class="label">退回时间</div>
		   <div class="field">
		     <input name="txnTm" id="txnTm" type="text" class="ui-datepicker" value="" validate="{required:true}">
           </div>
		</div>
		<div class="fieldBox col_50">
		   <div class="label">机构付款账户</div>
		   <div class="field">
		     <input name="recvAcc" id="recvAcc" type="text" class="ui-textbox" value="${recvAcc}" validate="{required:true}">
           </div>
		</div>
		<div class="fieldBox col_50">
		   <div class="label">我司收款账户</div>
		   <div class="field">
		     <input  name="acceptCode" id="acceptCode" type="text" class="ui-textbox" value="" onclick="queryMy()" validate="{required:true}">
		     <input  name="payAcc" id="payAcc"  style="display:none"  validate="{required:true}">
           </div>
		</div>
	</div>
	
	 <div class="btnsBox" align="center">
		<span id="mrgn_bsc_info_add_btn_id"
			class="sq-button sq-button-round" onclick="save();">保存
		</span>
		<span id="mrgn_return_info_btn_id"
			class="sq-button sq-button-round" onclick="closeWind();">关闭
		</span>
	 </div>
	</form> 
	 <!-- hidden -->
	<input name="fncInsId" id="fncInsId" type="hidden" class="ui-textbox" value="${fncInsId}"  >
	<input name="fncPrjId" id="fncPrjId" type="hidden" class="ui-textbox" value=""  >
	<input name="prjLgcSn" id="prjLgcSn" type="hidden" class="ui-textbox" value="${prjLgcSn}"  >
	<input name="srplsMrgn" id="srplsMrgn" type="hidden" class="ui-textbox" value=""  >
	<input name="lgcId" id="lgcId" type="hidden" class="ui-textbox" value="${lgcId}"  > 
</div>
<!-- 嵌入账户详情 -->
<jsp:include page="showAcc.jsp"></jsp:include>
<script type="text/javascript">
   var addAndUpdate="";
   $(function() {
	  initRecvList();
	  initPayList();
	  $("#formMrgnReturnInfo").ligerForm({
		     validate: true
		 });
	  initData();
	 });
   
   function initData(){
	      var lgcId=$("#lgcId").val();
	      if(lgcId==null||lgcId==""){
	    	  addAndUpdate="add";
	    	  var prjLgcSn=$("#prjLgcSn").val();
			  if(!prjLgcSn)  return;
			  var _url = '<%=path%>/rfbase/mrgnmanageinfo/getMrgnReturnInfoByLgcId.do';
			  AjaxUtils.ajaxPost(_url, {prjLgcSn:prjLgcSn}, callback);
	      }else{
	    	  
	    	  addAndUpdate="update";
	    	  var _url = '<%=path%>/rfbase/mrgnmanageinfo/getMrgnReturnInfoByMgrnLgcId.do';
			  AjaxUtils.ajaxPost(_url, {lgcId:lgcId}, callback);
	      }
		}
			
		  //新增回调
	   function callback(msg){
		  if(msg.success == 1){
			  setData(msg.data);//设置数据
		  }else{
			  UIUtils.alertError(msg.info);
		  }
	   }
			
		//设置页面数据
	   function setData(data){
			var mrgnManageInfo=data.mrgnManageInfo;
		    var mrgnDetailInfoList=mrgnManageInfo.mrgnDetailInfoList;
		    $("#fncInsNm").html(mrgnManageInfo.fncInsNm);
		    $("#srplsMrgnShow").html(moneyChange(mrgnManageInfo.srplsMrgn));
			$("#fncPrjNm").html(mrgnManageInfo.fncPrjNm);
			$("#fncPrjIdShow").html(mrgnManageInfo.fncPrjId);
			$("#mrgnAtm").html(moneyChange(mrgnManageInfo.mrgnAtm));
			$("#payTm").html(renderDateChange(mrgnManageInfo.payTm));
			$("#srplsMrgn").val(mrgnManageInfo.srplsMrgn);
			$("#fncPrjId").val(mrgnManageInfo.fncPrjId);
			$("#prjLgcSn").val(mrgnManageInfo.id);
			if( addAndUpdate=="update"){
			   var mrgnReturnInfo=data.mrgnReturnInfo;
			   var form = new liger.get("formMrgnReturnInfo");
			   form.setData({
				  txnAmt : mrgnReturnInfo.txnAmt,  
				  recvAcc : mrgnReturnInfo.recvAcc,
				  payAcc : mrgnReturnInfo.payAcc,
				  txnTm : renderDateChange(mrgnReturnInfo.txnTm)
			   });
			}
			f_showMrgnReturnInfo(mrgnDetailInfoList);
	  };

	  function f_getDetailData(listChange)
	  {
	      var data = { Rows: [] };
	      for (var i = 0; i < listChange.length; i++)
	      {
	         data.Rows.push(listChange[i]);
	      }
	      return data;
	  }
	  
	  //显示退回记录
	  function f_showMrgnReturnInfo(mrgnDetailInfoList)
	  { 
		    $("#mrgnReturnligergrid").ligerGrid({
		        columns: [
		            { display: '申请退回金额',name: "txnAmt", width: 150, render: function (rowdata, rowindex, value, column) { 
		            	var money =moneyChange(value);
	                    return money;
	            		},
	        	      enabledSort:true},   
		            { display: '到账金额', name: 'txnAmt', width: 150, render: function (rowdata, rowindex, value, column) { 
		            	var money =moneyChange(value);
	                    return money;
	            		},
	        	      enabledSort:true}, 
		            { display: '到账时间', name: 'txnTm', width: 150 , render:renderDate},
		            { display: '机构收款账号', name: 'reAccNum', width: 150, 
		            	render:function (rowdata, rowindex, value, column) { 
		            	var h ="<a href='#' onclick='showInsAcc("+rowdata.recvAcc+")'> <font color='blue'>"+rowdata.reAccNum+"</font></a>";
	                    return h;
	            		}
		            }, 
		            { display: '机构收款单位', name: 'reBanAccNm', width: 150, render:renderNull},
		            { display: '机构收款开户行行名', name: 'reAccNm', width: 200, render:renderNull},
		           
		            { display: '我司付款账号', name: 'payAccNum', width: 200, 
		            	render:function (rowdata, rowindex, value, column) { 
			            	var h ="<a href='#' onclick='showSysAcc("+rowdata.payAcc+")'> <font color='blue'>"+rowdata.payAccNum+"</font></a>";
		                    return h;
		            		}
		            },
		            { display: '我司付款单位', name: 'payBanAccNm', width: 200, render:renderNull},
		            { display: '我司付款开户行行名', name: 'payAccNm', width: 150, render:renderNull}
		            
		        ],
		        usePager:false,
		        data:f_getDetailData(mrgnDetailInfoList),
		        width:'100%',
		        height:'auto',
		        heightDiff:-2,
		        checkbox:false,
		        rownumbers:true,
		    });
	  }

   function initRecvList(){ 
   	   var instid = $("#fncInsId").val();
   	   $("#recvAcc").ligerComboBox({
             columns: [
 	                { header: '开户行号', name: 'banAccNum', width: 140}, 
 	                { header: '开户行名', name: 'banAccNm', width: 140}, 
 	                { header: '账户名称', name: 'accNm', width: 120},
 	                { header: '账号', name: 'accNum', width: 120}
 	               ],
 	            url:'<%=path%>/rfbase/mrgnmanageinfo/initRecvList.do?insId='+instid,
 	            ajaxType :'GET',
 	            width: 200, slide: false,
 	            selectBoxWidth: 680, selectBoxHeight: 240,
 	            valueField:'id',textField: 'accNum'     
 	       });
      }
   
     function initPayList(){
   	   var id = $("#payAcc").val();
	   	$.ajax({
			type: 'get',
			dataType:'json', 
			
			url: "<%=path%>/system/sysBkAccInfo/getById.do?id="+id,
			
			success:function(msg){
				 if(msg.success == 1){
					 $("#acceptCode").val(msg.data.accNo);
				 }
			}
		});
     }
 	//选择我司账户
 	function queryMy(){
 		
         $.ligerDialog.open({  title: '我司账户',height: 500,width: 850, url: '<%=path%>/system/sysBkAccInfo/toChooseListPage2.do?flag=dk'});
 	}
 	//选择我司账户设置数据
 	function setAccInfo(accNo,accNm,bkNm,bkNo,id){
 		$("#acceptCode").val(accNo);
 		
 		$("#payAcc").val(id);
 		
 	}
   function save() {
	   var ligerForm = liger.get("formMrgnReturnInfo");
	   //添加表单验证校验-JL-20181126
		if (!ligerForm.valid()) {
			top.$.ligerDialog.SQalert({content: "验证不通过，请修改表单信息！",SQ_type: "warning"});
			return false; //验证不通过，不执行以下代码
		}
  	   var data = ligerForm.getData();
  	   var txnAmt=data["txnAmt"];
	   var prjLgcSn = $("#prjLgcSn").val();
	   var srplsMrgn = $("#srplsMrgn").val();
	   var srplsAtm =srplsMrgn-txnAmt;
	   if(txnAmt<=0){
		   UIUtils.alertError("退回金额应大于0，请检查");
    	   return false;
	   }
	   if(($("#recvAcc").val()==null||$("#recvAcc").val()=="")&&($("#payAcc").val()==null||$("#payAcc").val()=="")){
	    	 UIUtils.alertError("“机构收款账户”与“我司付款账户”同时未填写,无法提交！");
		    	return false; 
	     }
	   if(data["txnTm"]==null||data["txnTm"]==""){
		   UIUtils.alertError("退回时间未填写，请检查");
    	   return false;
	   }
 //      if(srplsAtm<0){
//    	   UIUtils.alertError("退回金额大于剩余保证金金额，请检查");
 //   	   return false;
 //      }
       
       data["prjLgcSn"]=prjLgcSn;
       data["srplsAtm"]=srplsAtm;
       var lgcId=$("#lgcId").val();
       data["lgcId"]=lgcId;
       var url='<%=path%>/rfbase/mrgnmanageinfo/mrgnReturnUpdate.do';
       if(data["lgcId"]==null||data["lgcId"]==''){
    	   url='<%=path%>/rfbase/mrgnmanageinfo/mrgnReturnAdd.do';
		}
       //添加
      $.ajax({
    		type: 'post',
    		dataType:'json', 
    		data:data,
    		url: url,
    		beforeSend: function(){
    	          wait_dialog=$.ligerDialog.SQalert({content: LogUtils.LOG_CMM_DATA_LOADING ,SQ_type: "waitting" });
    	     },
    		success:function(msg){
    			 wait_dialog.close();
    			 var e=msg.success;
    			 if(msg.success == 1){
    				 var lgcId=$("#lgcId").val();
    				 if(lgcId==null||lgcId==''){
    					$("#lgcId").val(msg.data);
    					addAndUpdate="update";
    					startFlow();
    				 }else{
    					$.ligerDialog.success(msg.info); 
    				 }
    			 }else{
    				 $.ligerDialog.error(msg.info);
    			 }
    		}
    	});
   }
   
   //发起审核流程
   function startFlow(){
	   var _id=$("#lgcId").val();
		if(!_id){
		   UIUtils.alertError("请先保存再发起流程！");
		   return ;
		}
		var modelTyp ="R010";//流程业务分类-再融资保证金退回流程
		if(StringUtils.isEmpty(modelTyp)){
			console.log("modelTyp is null");
			return;
		}
    var flowDialog = $.ligerDialog.SQopen({
        url: '<%=path%>/flow/flow/toForStartListPage.do?modelTyp='+modelTyp,
        width: 700,
        height:400,
        title : '请选择要发起的流程',allowClose:false,
        buttons: [{
	                text: "确定", type: 'ok', cls: 'l-dialog-btn-ok',
	                onclick: function (item, Dialog) {
	                    if (Dialog.onOk) Dialog.onOk();
	                }
	            }, {
	                text: "取消", type: 'cancel', cls: 'l-dialog-btn-no',
	                onclick: function (item, Dialog) {
	                    if (Dialog.onCancel) Dialog.onCancel();
	                }
	            }
	        ],
        onClose: function () {
        	var json = flowDialog._DATA;
        	console.log(json);
        	if(!json){
        		closeWind();
            	return;
        	}
        	var wait_dialog=$.ligerDialog.SQalert({content: LogUtils.LOG_CMM_DATA_LOADING, SQ_type: "waitting",onClose: function () {}});
    		$.ajax({
        		type: 'post',
        		dataType:'json',
        		data:{
        			id:_id,
        			procDefKey:json.procDefKey,
        			procDefId:json.procDefId,
        			procNo:json.procNo
        		},
        		url: '<%=path%>/rfbase/mrgnmanageinfo/addMrgnReturnFlowStart.do',
        		success:function(msg){
        			wait_dialog.close();
        			 if(msg.success == 1){
      					 //UIUtils.alertSuccessHold1(msg.info);
        				 top.$.ligerDialog.SQalert({content:msg.info, SQ_showButtons: true,SQ_type: "success"});
      					 //closeWind();
      					 Remove_navTab();//关闭申请窗口
           			 }else{
          				//UIUtils.alertErrorHold1(msg.info);
          				top.$.ligerDialog.SQalert({content:msg.info, SQ_showButtons: true,SQ_type: "error"});
           			 }
        		}
        	});
        }
    });
  }	
   
 //格式化时间
 function renderDateChange(value){
	var dateStr = "";
	if(null != value &&  ""!= value){
		var date = new Date(value);
		dateStr = DateUtils.formatDate(date, DateUtils.YYYY_MM_DDHHMMSS);
	}
	return dateStr;
 } 
 
//格式化null
 function renderNull(rowdata, index, value){
	var dateStr = "";
	if(null != value){
		var dateStr = value;
	}
	return dateStr;
 }
 
  //格式化时间
  function renderDate(rowdata, index, value){
	  var dateStr = "";
	  if(null != value){
		 var date = new Date(value);
		 dateStr = DateUtils.formatDate(date, DateUtils.YYYY_MM_DDHHMMSS);
	  }
	  return dateStr;
	}
  
  //关闭页面
  function closeWind(){
	var selectedTabId = parent.mainApp.parentTab.selectedTabId;
	var lastTab = parent.tabItems[0];
	parent.mainApp.parentTab.reload(lastTab.tabid);
	parent.mainApp.parentTab.removeTabItem(selectedTabId);
	parent.grid.reload();
  }
  
//格式化金额
  function moneyChange(value) { 
 		var h = '';
 			if(value&&value!="null"){
 		    h+=fmoney(value, 2);
 			}else{
 			h+="0.00";
 			}
          return h;
 		}
  
  //格式化金额
  function fmoney(s, n)   
 	{   
 	   n = n > 0 && n <= 20 ? n : 2;   
 	   s = parseFloat((s + "").replace(/[^\d\.-]/g, "")).toFixed(n) + "";   
 	   var l = s.split(".")[0].split("").reverse(),   
 	   r = s.split(".")[1];   
 	   t = "";   
 	   for(i = 0; i < l.length; i ++ )   
 	   {   
 	      t += l[i] + ((i + 1) % 3 == 0 && (i + 1) != l.length ? "," : "");   
 	   }   
 	   return t.split("").reverse().join("") + "." + r;   
 	} 
</script>
</body>
</html>
